<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>展示所有报名人员的信息</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
    <script th:src="@{/layui/layui.js}"></script>

</head>
<body>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="update">修改</a>
</script>
<form id="addStudentForm" class="layui-form" style="display: none">
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="name" required  lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">人员年龄</label>
        <div class="layui-input-block">
            <input type="text" name="age" required  lay-verify="required|number" placeholder="请输入人员年龄" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">学校或单位</label>
        <div class="layui-input-block">
            <input type="text" name="school" required  lay-verify="required" placeholder="请输入人员学校或单位" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input type="text" name="phone" required  lay-verify="required|phone" placeholder="请输入手机号" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveStudentFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<form id="updateStudentForm" class="layui-form" lay-filter="example"  style="display: none">
    <input type="hidden" name="id">
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="name" required  lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">人员年龄</label>
        <div class="layui-input-block">
            <input type="text" name="age" required  lay-verify="required|number" placeholder="请输入人员年龄" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">学校或单位</label>
        <div class="layui-input-block">
            <input type="text" name="school" required  lay-verify="required" placeholder="请输入人员学校或单位" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input type="text" name="phone" required  lay-verify="required|phone" placeholder="请输入手机号" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updateStudentFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<!--头部工具栏按钮-->
<script type="text/html" id="StudentToolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>添加
        </button>
       <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delSelected">
            <i class="layui-icon layui-icon-delete"></i>删除
        </button>
    </div>
</script>
<!--条件查询-->
<form class="layui-form" style="margin-top: 10px;margin-left: 10px" lay-filter="myDiv">
    活动名称：
    <div class="layui-inline">
        <div class="layui-input-block">
            <select id="businessSelect" name="businessSelect">

            </select>
        </div>
    </div>
    姓名：
    <div class="layui-inline">
        <input class="layui-input" name="name" placeholder="姓名" autocomplete="off">
    </div>
    手机号：
    <div class="layui-inline">
        <input class="layui-input" name="phone" placeholder="手机号" autocomplete="off">
    </div>

    <button class="layui-btn" lay-submit lay-filter="selectConditionFilter" data-type="reload">搜索</button>
    <button class="layui-btn layui-btn-primary" type="reset" id="resetButton">重置</button>
</form>
<table id="StudentTable" lay-filter="chenjian"></table>

<!--<script src="/layui/layui.js"></script>-->
<script>
    layui.use(['table','form','jquery'], function(){
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;

        //第一个实例
        var myTable= table.render({
            elem: '#StudentTable'
            ,height: 450
            ,url: '/student/findAllStudent' //数据接口
            ,page: true //开启分页
            ,toolbar: '#StudentToolbar'
            ,cols: [
                [ //表头
                    {type: 'checkbox', fixed: 'left'}
                ,{field: 'id', title: '报名编号', width:100, sort: true, fixed: 'left'}
                ,{field: 'name', title: '姓名', width:120}
                ,{field: 'age', title: '年龄', width:80, sort: true}
                ,{field: 'school', title: '学校或单位', width:150,sort: true}
                ,{field: 'phone', title: '手机号', width:120,sort: true}
                ,{field: 'createTime', title: '报名时间', width:170,sort: true}
                ,{field: 'businessId', title: '活动ID', width:170,sort: true,hide:"true"}
                ,{field: 'businessName', title: '活动名称', width:170,sort: true}
                ,{fixed: 'right', width: 150, align:'center', toolbar: '#barDemo',fixed: 'right'}
            ]]
        });

        let $businessSelect = $("#businessSelect");
        let options = '';
        //填充活动下拉框
        $.ajax({
            url: "/Business/findAllBusinessNoPage",
            type: "GET",
            dataType: "JSON",
            success: function (allBusiness) {
                options +=' <option value="">全选</option>';
                //将数据放到select中
                for (let i = 0; i < allBusiness.length; i++) {

                    options += '<option value="' + allBusiness[i].id + '">' + allBusiness[i].name + '</option>';
                }
                $businessSelect.html(options)
                layui.form.render('select','myDiv');
            }
        })
        //条件查询
        form.on('submit(selectConditionFilter)', function (data) {
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            myTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    businessId: data.field.businessSelect
                    , name: data.field.name
                    , phone: data.field.phone
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        $("#resetButton").click(function () {
            myTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    businessId: ""
                    , name: ""
                    , phone: ""
                    //…
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        })

        //监听头工具栏事件
        table.on('toolbar(chenjian)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'add':
                  /*  layer.msg('讨厌！');*/
                    layer.open({
                        title: '添加人员',
                        type: 1,
                        area: ['600px', '400px'],
                        content: $("#addStudentForm")//这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
                case 'update':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else if(data.length > 1){
                        layer.msg('只能同时编辑一个');
                    } else {
                        layer.alert('编辑 [id]：'+ checkStatus.data[0].id);
                    }
                    break;
                case 'delSelected':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else {
                        //将选中的数据传到后端，进行数据库操作
                        console.log(data);
                        layer.confirm('真的删除选中的行么', function(index){
                            //删除数据库中的数据
                            console.log(data)
                            $.ajax({
                                url:"/student/deleteStudentByIds",
                                type: "POST",
                                dataType:"JSON",//从服务器返回的数据是json
                                contentType: 'application/json;charset=UTF-8',//发送给服务器的数据是json
                                data:JSON.stringify(data),//将选中的数据转换成json字符串
                                success:function (ret) {
                                    if(ret.code==0){
                                        //刷新表格数据
                                        myTable.reload();
                                        layer.msg(ret.msg,{icon: 6});
                                    }else
                                    {
                                        layer.msg(ret.msg,{icon: 5});
                                    }
                                }
                            })
                            layer.close(index);
                        });
                    }
                    break;
            };
        });


        form.on('submit(saveStudentFilter)', function(data){
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            //提交表单数据
            $.ajax({
                url:"/student/saveStudent",
                type: "POST",
                dataType:"JSON",
                data:data.field,
                success:function (ret) {
                    if(ret.code==0){
                        //关闭对话框
                        layer.closeAll();
                        //刷新表格数据
                        myTable.reload({
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                        layer.msg(ret.msg,{icon: 6});
                    }else
                    {
                        layer.msg(ret.msg,{icon: 5});
                    }

                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        form.on('submit(updateStudentFilter)', function(data){
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            //提交表单数据
            $.ajax({
                url:"/student/updateStudent",
                type: "POST",
                dataType:"JSON",
                data:data.field,
                success:function (ret) {
                    if(ret.code==0){
                        //关闭对话框
                        layer.closeAll();
                        //刷新表格数据
                        myTable.reload({
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                        layer.msg(ret.msg,{icon: 6});
                    }else
                    {
                        layer.msg(ret.msg,{icon: 5});
                    }

                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //监听行工具事件
        table.on('tool(chenjian)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'update'){
                console.log(data);
                //表单赋值
                form.val('example', {
                    "id": data.id
                    ,"name": data.name
                    ,"age": data.age
                    ,"school": data.school
                    ,"phone": data.phone
                });

                //修改人员信息
                layer.open({
                    title: '修改人员',
                    type: 1,
                    area: ['600px', '400px'],
                    content: $("#updateStudentForm")//这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });


            } else if(layEvent === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    //obj.del(); //删除对应行（tr）的DOM结构
                    //删除数据库中的数据
                    console.log(data)
                    $.ajax({
                        url: "/student/deleteStudentById",
                        type: "POST",
                        dataType: "JSON",
                        data: {id: data.id},
                        success: function (ret) {
                            if (ret.code == 0) {
                                //刷新表格数据
                                myTable.reload({
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                });
                                layer.msg(ret.msg, {icon: 6});
                            } else {
                                layer.msg(ret.msg, {icon: 5});
                            }
                        }
                    })
                    layer.close(index);
                    //向服务端发送删除指令

                })
            }
        });

    });
</script>
</body>
</html>